package com.atguigu.srb.sms.controller;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.utils.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Slf4j
@RestController
@RequestMapping("/api/sms")
/*@CrossOrigin*/
public class SmsController {
    @Resource
    private CoreUserInfoClient coreUserInfoClient;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SmsService smsService;
    @GetMapping("/send/{mobile}")
    public R send(@PathVariable String mobile){
        //手机号不能为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //手机号不正确
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        //判断手机号是否注册过
        boolean flag = coreUserInfoClient.checkMobile(mobile);
        Assert.isTrue(flag,ResponseEnum.MOBILE_EXIST_ERROR);
        //发送验证码
         smsService.send(mobile);
        //将验证码存入redis
        return R.ok().message("短信发送完成");
    }


}
